package com.wanwu.channel.converter;

import com.wanwu.channel.common.enums.BizEnum;
import com.wanwu.channel.common.enums.ChannelPayStatus;
import com.wanwu.channel.common.util.GetSerialNoUtils;
import com.wanwu.channel.common.util.IPUtil;
import com.wanwu.channel.dao.entity.ChannelPayEntity;
import com.wanwu.channel.dto.vo.PayQueryStrategyReqVo;
import com.wanwu.channel.dto.vo.PrepayStrategyReqVo;
import com.wanwu.channel.facade.dto.req.ChPrepayReqDto;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
 * @ClassName ChannelPayConvert
 * @Description 支付实体转换
 * @Author husheng
 * @Date 2020-01-16 19:33
 */
public class ChannelPayConvert {

    /**
     * 预下单转入库实体
     * @param gatewayPrepayRequest
     * @return
     */
    public static ChannelPayEntity buildInsertChannelPay(ChPrepayReqDto gatewayPrepayRequest) {
        ChannelPayEntity channelPay = new ChannelPayEntity();
        channelPay.setPayType(gatewayPrepayRequest.getPayType());
        channelPay.setBizOrderNo(gatewayPrepayRequest.getBizOrderNo());
        channelPay.setBizCode(gatewayPrepayRequest.getBizCode());
        channelPay.setChannelNo(GetSerialNoUtils.getChannelNo(BizEnum.PAY.getFlg()));
        channelPay.setBusinessNotifyUrl(gatewayPrepayRequest.getBusinessNotifyUrl());
        channelPay.setCurrency(gatewayPrepayRequest.getCurrency());
        channelPay.setIp(IPUtil.firstIp());
        channelPay.setRmbAmount(gatewayPrepayRequest.getPayAmount().longValue());
        channelPay.setSubject(gatewayPrepayRequest.getSubject());
        channelPay.setPayStatus(ChannelPayStatus.PROCESSING.getValue());
        channelPay.setUserId(gatewayPrepayRequest.getUserId());
        channelPay.setMerchantNo(gatewayPrepayRequest.getMerchantNo());
        channelPay.setAppId(gatewayPrepayRequest.getMerchantAppId());
        channelPay.setThirdFlag(1);
        return channelPay;
    }

    /**
     * 组支付渠道公共入参
     * @param gatewayPrepayRequest
     * @return
     */
    public static PrepayStrategyReqVo buildPrepayStrategyReq(ChPrepayReqDto gatewayPrepayRequest) {
        PrepayStrategyReqVo req = new PrepayStrategyReqVo();
        req.setMerPageUrl(gatewayPrepayRequest.getFrontUrl());
        req.setPayAmount(gatewayPrepayRequest.getPayAmount());
        req.setCurrency(gatewayPrepayRequest.getCurrency());
        req.setBizOrderNo(gatewayPrepayRequest.getBizOrderNo());
        req.setTotalNumber(gatewayPrepayRequest.getTotalQuantity());
        req.setGoodsDetailDescription(gatewayPrepayRequest.getGoodsDetailDescrption());
        String goodsInfo = "";
        if (CollectionUtils.isNotEmpty(gatewayPrepayRequest.getBody())) {
            for (ChPrepayReqDto.Product p : gatewayPrepayRequest.getBody()) {
                goodsInfo = goodsInfo + p.getSku() + "^" + p.getSkuNum() + "|";
            }
        }
        if (goodsInfo.endsWith("|")) {
            goodsInfo = goodsInfo.substring(0, goodsInfo.length() - 1);
        }
        req.setGoodsInfo(goodsInfo);
        req.setSubAppId(gatewayPrepayRequest.getAppid());
        req.setAcct(gatewayPrepayRequest.getAcct());
        req.setCusId(gatewayPrepayRequest.getMerchantNo());
        req.setAppId(gatewayPrepayRequest.getMerchantAppId());

        req.setAttach(gatewayPrepayRequest.getAttach());
        req.setSpbillCreateIp(gatewayPrepayRequest.getSpbillCreateIp());
        req.setSceneCode(gatewayPrepayRequest.getSceneCode());
        req.setSceneName(gatewayPrepayRequest.getSceneName());
        if(StringUtils.isNotEmpty(gatewayPrepayRequest.getSubject()) && gatewayPrepayRequest.getSubject().length() > 50){
            req.setOrderName(gatewayPrepayRequest.getSubject().substring(0, 50));
        }else{
            req.setOrderName(gatewayPrepayRequest.getSubject());
        }
        req.setSubject(gatewayPrepayRequest.getSubject());
        return req;
    }

    /**
     * 组渠道公共查询入参
     * @param channelPay
     * @return
     */
    public static PayQueryStrategyReqVo buildPayQueryReq(ChannelPayEntity channelPay) {
        PayQueryStrategyReqVo req = new PayQueryStrategyReqVo();
        req.setBizOrderNo(channelPay.getBizOrderNo());
        req.setChannelNo(channelPay.getChannelNo());
        req.setThirdFlag(channelPay.getThirdFlag());
        req.setThirdOrderNo(channelPay.getThirdOrderNo());
        req.setTrxId(channelPay.getTlOrderNo());
        req.setPayOrderCreateTime(channelPay.getCreateTime());
        return req;
    }

}
